package com.app.business.web.business.newTextForCs;

import com.app.business.model.newTextForCs.TextContract;
import com.app.business.service.newTextForCs.TextContractService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 测算基础信息控制层
 * Created by ddz on 2020/1/20.
 */
@Controller
@RequestMapping("/business/newTextForCs/textContract/*")
@SuppressWarnings("all")
public class TextContractController {

    @Autowired
    private TextContractService textContractService;
    @Autowired
    private DataDictService dataDictService;


    /**
     * 测试基础数据管理列表
     */
    @RequestMapping("textContractList")
    @RequiresPermissions("textContract:textContractList")
    public void textContractList() {
    }

    /**
     * 加载基础信息数据
     *
     * @return
     */
    @RequestMapping("loadTextContractList")
    @ResponseBody
    @RequiresPermissions("textContract:loadTextContractList")
    public Map loadTextContractList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<TextContract> textContractPageBean = this.textContractService.getTextContractPageBean();
        List<TextContract> textContractList = textContractPageBean.getDatas();


        if (CollectionUtils.isNotEmpty(textContractList)) {
            Map<String, Object> item = null;
            for (TextContract textContract : textContractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", textContract.getId());
                item.put("projectCode", StringUtils.getNotNull(textContract.getProjectCode()));
                item.put("projectName", StringUtils.getNotNull(textContract.getProjectName()));
                item.put("contractCode", StringUtils.getNotNull(textContract.getContractCode()));
                item.put("name", StringUtils.getNotNull(textContract.getName()));
                item.put("chooseNum", StringUtils.getNotNull(textContract.getChooseNum()));
                item.put("kazArea", BigDecimalUtils.getNotNullStr(textContract.getKazArea()));

                listData.add(item);
            }
        }
        return LayUiUtils.page(textContractPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 导入列表
     */
    @RequestMapping("importList")
    @RequiresPermissions("textContract:importList")
    public void importList() {
    }

    /**
     * 读取Excel表格
     *
     * @param request
     * @return
     * @throws Exception
     */
    @Transactional
    @RequestMapping("importAll")
    @ResponseBody
    @RequiresPermissions("textContract:importAll")
    public Response importAll(HttpServletRequest request) throws Exception {
        String textContracts = request.getParameter("textContractArray");
        String[] textContractArray = textContracts.split("\r\n");

//        List<String> textContractCodeList = textContractService.getTextContractCodeList();

        String projectCode = null, projectName = null, contractCode = null, name = null, chooseNum = null, kazArea = null;
        TextContract textContract = null;

        List<TextContract> textContractImportList = new ArrayList<>();
        int rowObjLength = 0;//每条数据的列数

        String[] rowObj = null;
        StringBuffer msg = new StringBuffer();
        int index = 0;
        for (String textContractInfo : textContractArray) {
            rowObj = null;
            rowObj = textContractInfo.split("\t");
            rowObjLength = rowObj.length;
            Precondition.checkAjaxArguement(rowObjLength == 6, "0012", "请确认每列都有数据！");
            if (rowObj.length == 0) {
                continue;
            }

            index = 0;
            projectCode = rowObj[index]; //项目编号
            index++;
            projectName = rowObj[index]; //项目名称
            index++;
            contractCode = rowObj[index]; //协议编号
            index++;
            name = rowObj[index]; //被征收人
            index++;
            chooseNum = rowObj[index]; //选房号
            index++;
            kazArea = rowObj[index]; //可安置面积

//            //只能导入编号不存在
//            if (textContractCodeList.contains(contractCode)) {
//                //已经存在的数据输出到页面
//                msg.append("<br/>" + textContractInfo + ",");
//                continue;
//            }

            textContract = new TextContract();

            textContract.setProjectCode(projectCode);
            textContract.setProjectName(projectName);
            textContract.setContractCode(contractCode);
            textContract.setName(name);
            textContract.setChooseNum(chooseNum);
            textContract.setKazArea(BigDecimalUtils.stringToBigDecimal(kazArea));

            textContractImportList.add(textContract);
        }

        this.textContractService.batchSave(textContractImportList);
        String successInfo = "插入" + textContractImportList.size() + "条数据;";
        msg = msg.length() == 0 ? msg : msg.replace(0, msg.length(), msg.toString()).append("<br/>数据已存在无法导入");
        Response response = new Response("0001", successInfo + msg.toString());
        return response;
    }

}
